#include "VSAdditiveAnimBlend.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSAdditiveAnimBlend,VSOneParamAnimBlend)
static bool gs_bStreamRegistered_VSAdditiveAnimBlend= VSAdditiveAnimBlend::RegisterMainFactory();
bool VSAdditiveAnimBlend::ms_bRegisterMainFactory = false;
bool VSAdditiveAnimBlend::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 
		VSMain::AddInitialFuntion(VSAdditiveAnimBlend::InitialClassFactory); 
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
bool VSAdditiveAnimBlend::InitialClassFactory()
{
	ms_ClassFactory.AddElement(ms_Type.GetName(),FactoryFunc);

	return 1;
}
VSObject * VSAdditiveAnimBlend::FactoryFunc()
{
	return VS_NEW VSAdditiveAnimBlend;

}
VSAdditiveAnimBlend::~VSAdditiveAnimBlend()
{

}
VSAdditiveAnimBlend::VSAdditiveAnimBlend()
{
	m_fParam = 1.0f;
	m_fParamMax = 1.0f;
	m_fParamMin = 0.0f;
}
VSAdditiveAnimBlend::VSAdditiveAnimBlend(const VSUsedName & ShowName,VSAnimTree * pAnimTree)
:VSOneParamAnimBlend(ShowName,pAnimTree)
{
	m_fParam = 1.0f;
	m_fParamMax = 1.0f;
	m_fParamMin = 0.0f;


	VSString InputName0 = _T("Child0");
	VSInputNode * pInputNode = NULL;
	pInputNode = VS_NEW VSInputNode(VSPutNode::AVT_ANIM,InputName0,this);
	VSMAC_ASSERT(pInputNode);
	m_pInput.AddElement(pInputNode);
	//m_NodeCrossFadingTime.AddElement(0.0f);

	VSString InputName1 = _T("Child1");
	pInputNode = NULL;
	pInputNode = VS_NEW VSInputNode(VSPutNode::AVT_ANIM,InputName1,this);
	VSMAC_ASSERT(pInputNode);
	m_pInput.AddElement(pInputNode);
	//m_NodeCrossFadingTime.AddElement(0.0f);
}
void VSAdditiveAnimBlend::AddInputNode()
{
	return;
}
void VSAdditiveAnimBlend::DeleteInputNode()
{
	return ;
}
bool VSAdditiveAnimBlend::ComputeOutBoneMatrix(double dAppTime)
{
	VSREAL fInternal = m_fParamMax - m_fParamMin;
	VSREAL fInternalSeg = fInternal / (m_pInput.GetNum() - 1);
	if (fInternalSeg < EPSILON_E4)
	{
		return 0;
	}
	unsigned int uiIndex1 = 0;
	unsigned int uiIndex2 = 0;

	float fTemp = (m_fParam - m_fParamMin) / fInternalSeg;
	uiIndex1 = (unsigned int)fTemp;

	if (uiIndex1 >= m_pInput.GetNum() - 1)
	{
		uiIndex2 = uiIndex1;
	}
	else
	{
		uiIndex2 = uiIndex1 + 1;
	}


	VSREAL fWeight = fTemp - (float)uiIndex1;


	VSInputNode* pInputNode1 = GetInputNode(uiIndex1);
	VSInputNode* pInputNode2 = GetInputNode(uiIndex2);
	if(pInputNode1->GetOutputLink() && pInputNode2->GetOutputLink())
	{
		VSAnimFunction *pAnimBaseFunction1 = (VSAnimFunction *)pInputNode1->GetOutputLink()->GetOwner();
		VSAnimFunction *pAnimBaseFunction2 = (VSAnimFunction *)pInputNode2->GetOutputLink()->GetOwner();
		LineBlendTwo(this,pAnimBaseFunction1,pAnimBaseFunction2,fWeight);
	}

	

	return 1;
}